package org.footito.android.data.update;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class Waiter extends AsyncTask<Void, Integer, Void> implements UpdateListener {

	private Exception error;
	private UpdateListener listener;
	private Context context;

	public Waiter(Context context, UpdateListener listener) {
		this.listener = listener;
		this.context = context;
	}

	@Override
	protected Void doInBackground(Void... params) {
		//Log.d("Footito", "updatingCompleteProcess");
		DataManager.updatingCompleteProcess(context, this);
		//Log.d("Footito", "updatingCompleteProcess ok");

		return null;
	}

	@Override
	protected void onCancelled() {
		//Log.d("Footito", "onCancelled");
		super.onCancelled();
	}

	@Override
	public void onComplete() {
		//Log.d("Footito", "onComplete");
	}

	@Override
	public void onError(Exception e) {
		Log.e("Footito", e.getLocalizedMessage());
		error = e;
	}

	@Override
	protected void onPostExecute(Void result) {
		//Log.d("Footito", "Waiter ended ");
		if (error != null) {
			listener.onError(error);
		} else {
			listener.onComplete();
		}
	}

	@Override
	protected void onPreExecute() {
		//Log.d("Footito", "onPreExecute");
		super.onPreExecute();
	};

	@Override
	public void onProgress(Integer... progressId) {
		publishProgress(progressId);
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		listener.onProgress(values);
	}

};